package cc.higou.service.message;

import cc.higou.service.SolrService;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.io.IOException;

/**
 * 说明：
 * 自定义消息监听器
 *
 * @author:dongxing
 * @time:--
 */

public class MyMessageListener implements MessageListener {

    @Autowired
    private SolrService solrService;

    /**
     * 当监听到消息后，会自动调用此方法
     */
    @Override
    public void onMessage(Message message) {
        //将Message强转为ActiveMQTextMessage
        ActiveMQTextMessage activeMQTextMessage = (ActiveMQTextMessage) message;


        try {
            //获取ActiveMQ中的信息
            String ids = activeMQTextMessage.getText();
            System.out.println("消费方接收到的消息：" + ids);

            solrService.addProduct(ids);
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
